package com.inclass.security;

import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.cas.CasFilter;
import org.apache.shiro.subject.Subject;

import com.neusoft.education.tp.sso.client.CASReceipt;
import com.neusoft.education.tp.sso.client.filter.CASFilter;

public class MyCasFilter extends CasFilter{
	// the name of the parameter service ticket in url
	private static final String TICKET_PARAMETER = "ticket";

	protected AuthenticationToken createToken(ServletRequest request,
			ServletResponse response) throws Exception {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		String ticket = httpRequest.getParameter(TICKET_PARAMETER);
		HDUCASToken token = new HDUCASToken(ticket); 
		token.setRememberMe(true);
		CASReceipt receipt = (CASReceipt) httpRequest.getSession().getAttribute(CASFilter.SSO_FILTER_RECEIPT);
		Map<String, String> map = null;
		if(receipt != null){
			receipt.getUserName();
			map = receipt.getUserMap();
			token.setUserMap(map);
			String  userID = map.get("user_id");
			token.setUserId(userID);
		}
		return token;
	}

	@Override
	protected boolean onLoginFailure(AuthenticationToken token,
			AuthenticationException ae, ServletRequest request,
			ServletResponse response) {
		// TODO Auto-generated method stub
		return super.onLoginFailure(token, ae, request, response);
	}
	@Override
	public boolean onPreHandle(ServletRequest request,
			ServletResponse response, Object mappedValue) throws Exception {
		// TODO Auto-generated method stub
		return super.onPreHandle(request, response, mappedValue);
	}
	@Override
	protected boolean executeLogin(ServletRequest request,
			ServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		return super.executeLogin(request, response);
	}
	@Override
	protected boolean onLoginSuccess(AuthenticationToken token,
			Subject subject, ServletRequest request, ServletResponse response)
					throws Exception {
			return super.onLoginSuccess(token, subject, request, response);
	}
}
